package com.copplay.system.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.copplay.common.core.constant.CacheNames;
import com.copplay.common.core.utils.MapstructUtils;
import com.copplay.common.core.utils.ObjectUtils;
import com.copplay.common.mybatis.core.page.PageQuery;
import com.copplay.system.domain.DictData;
import com.copplay.common.mybatis.core.page.TableDataInfo;
import com.copplay.common.core.exception.ServiceException;
import com.copplay.common.core.utils.StringUtils;
import com.copplay.common.redis.utils.CacheUtils;
import com.copplay.system.web.bo.DictDataBo;
import com.copplay.system.web.vo.DictDataVo;
import com.copplay.system.mapper.DictDataMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 字典 业务层处理
 *
 * @author Lion Li
 */
@RequiredArgsConstructor
@Service
public class SysDictDataServiceImpl implements SysDictDataService {

    private final DictDataMapper baseMapper;

    @Override
    public TableDataInfo<DictDataVo> selectPageDictDataList(DictDataBo dictData, PageQuery pageQuery) {
        LambdaQueryWrapper<DictData> lqw = buildQueryWrapper(dictData);
        Page<DictDataVo> page = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    /**
     * 根据条件分页查询字典数据
     *
     * @param dictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public List<DictDataVo> selectDictDataList(DictDataBo dictData) {
        LambdaQueryWrapper<DictData> lqw = buildQueryWrapper(dictData);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DictData> buildQueryWrapper(DictDataBo bo) {
        LambdaQueryWrapper<DictData> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDictSort() != null, DictData::getDictSort, bo.getDictSort());
        lqw.like(StringUtils.isNotBlank(bo.getDictLabel()), DictData::getDictLabel, bo.getDictLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getDictType()), DictData::getDictType, bo.getDictType());
        lqw.orderByAsc(DictData::getDictSort);
        return lqw;
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType  字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue) {
        return baseMapper.selectOne(new LambdaQueryWrapper<DictData>()
                .select(DictData::getDictLabel)
                .eq(DictData::getDictType, dictType)
                .eq(DictData::getDictValue, dictValue))
            .getDictLabel();
    }

    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public DictDataVo selectDictDataById(Long dictCode) {
        return baseMapper.selectVoById(dictCode);
    }

    /**
     * 批量删除字典数据信息
     *
     * @param dictCodes 需要删除的字典数据ID
     */
    @Override
    public void deleteDictDataByIds(Long[] dictCodes) {
        for (Long dictCode : dictCodes) {
            DictData data = baseMapper.selectById(dictCode);
            baseMapper.deleteById(dictCode);
            CacheUtils.evict(CacheNames.DICT, data.getDictType());
        }
    }

    /**
     * 新增保存字典数据信息
     *
     * @param bo 字典数据信息
     * @return 结果
     */
    @CachePut(cacheNames = CacheNames.DICT, key = "#bo.dictType")
    @Override
    public List<DictDataVo> insertDictData(DictDataBo bo) {
        DictData data = MapstructUtils.convert(bo, DictData.class);
        int row = baseMapper.insert(data);
        if (row > 0) {
            return baseMapper.selectDictDataByType(data.getDictType());
        }
        throw new ServiceException("操作失败");
    }

    /**
     * 修改保存字典数据信息
     *
     * @param bo 字典数据信息
     * @return 结果
     */
    @CachePut(cacheNames = CacheNames.DICT, key = "#bo.dictType")
    @Override
    public List<DictDataVo> updateDictData(DictDataBo bo) {
        DictData data = MapstructUtils.convert(bo, DictData.class);
        int row = baseMapper.updateById(data);
        if (row > 0) {
            return baseMapper.selectDictDataByType(data.getDictType());
        }
        throw new ServiceException("操作失败");
    }

    /**
     * 校验字典键值是否唯一
     *
     * @param dict 字典数据
     * @return 结果
     */
    @Override
    public boolean checkDictDataUnique(DictDataBo dict) {
        Long dictCode = ObjectUtils.notNull(dict.getDictCode(), -1L);
        DictData entity = baseMapper.selectOne(new LambdaQueryWrapper<DictData>()
            .eq(DictData::getDictType, dict.getDictType()).eq(DictData::getDictValue, dict.getDictValue()));
        if (ObjectUtil.isNotNull(entity) && !dictCode.equals(entity.getDictCode())) {
            return false;
        }
        return true;
    }

}
